from OWRpy import *
import redRi18n
_ = redRi18n.get_(package = 'base')

import signals 
import redRGUI
# widget code
class scaleComp(OWRpy):
    settingsList = []
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)

        # Unique R variables
        self.setRvariableNames(['scaleComp'])
        
        # Input
        self.inputs.addInput('Data Table', 'Data Table', signals.base.RMatrix, self.processx)
        
        # Output
        self.outputs.addOutput('Data Table', 'Data Table', signals.base.RMatrix) # trace problem with outputs

        #GUI
        area = redRGUI.base.widgetBox(self.controlArea,orientation='vertical')  
        box = redRGUI.base.groupBox(area, label = "Options")        
        self.selectedOperation = checkBox(box,  label = "Operation", 
        buttons = [('Scale'),('Center')],orientation='vertical')
        self.commit = redRGUI.base.commitButton(self.bottomAreaRight, _("Commit"), alignment=Qt.AlignRight, 
        callback = self.commitFunction, processOnInput=True)


    def processx(self, data):
        if not self.require_librarys(["compositions"]):
            self.status.setText('R Libraries Not Loaded.')
            return
        if data:
            self.data = data.getData()
            self.commitFunction()
        else:
            return

    def commitFunction(self):
         
        if 'Scale' in self.selectedOperation.getChecked():
            self.selectedScale='TRUE'
        else:
            self.selectedScale='FALSE'
        if 'Center' in self.selectedOperation.getChecked():
            self.selectedCenter='TRUE'
        else:
            self.selectedCenter='FALSE'
        
        self.R(self.Rvariables['scaleComp']+'<-scale('+self.data+', scale='+self.selectedScale+', center='+self.selectedCenter+')')
        self.newData = signals.base.RMatrix(self, data = self.Rvariables['scaleComp'], checkVal = False)
        self.rSend('Data Table', self.newData)
            